#include "BSP_Gpio.h"

/**
 *******************************************************************************
 ** \brief GPIO IO输出电平翻转
 **
 ** \param [in]  enPort          IO Port口
 ** \param [in]  enPin           IO Pin脚
 ******************************************************************************/
static void Gpio_ToggleOutputIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
	GPIO_WriteBit(GPIOx,GPIO_Pin,GPIO_ReadInputDataBit(GPIOx, GPIO_Pin)?Bit_RESET:Bit_SET);
}
/**
 *******************************************************************************
 ** \brief GPIO IO输出模式初始化
 **
 ** \param [in]  enPort          IO Port口
 ** \param [in]  enPin           IO Pin脚
 ** \param [in]  pstcGpioCfg     IO 配置结构体指针
 ** \param [out] bVal            缺省输出值
 ******************************************************************************/
static void GpioCfgOutput(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin, GPIO_InitTypeDef  GPIO_InitStructure,BitAction bVal)
{
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin;				
			GPIO_Init(GPIOx,&GPIO_InitStructure);		
		GPIO_WriteBit(GPIOx, GPIO_Pin, bVal);

}

///**
// *******************************************************************************
// ** \brief GPIO IO模拟模式初始化
// **
// ** \param [in]  enPort          IO Port口
// ** \param [in]  enPin           IO Pin脚
// ** \param [in]  pstcGpioCfg     IO 配置结构体指针
// ******************************************************************************/
//static void GpioCfgAnalog(en_gpio_port_t enPort, en_gpio_pin_t enPin, stc_gpio_cfg_t  *pstcGpioCfg)
//{
//    Gpio_SetAnalogMode(enPort, enPin);
//    Gpio_Init(enPort, enPin, pstcGpioCfg);
//}


///************************************************************************/
///*      UFD Registration                                                */
///************************************************************************/
//#include    "UFD.h"
//static UFD_CmdRec_t sUFDCmdTbl[] =
//{
//UFD_HDR( "BSP_GPIO",   "Driver for BSP_GPIO" ),

//UFD_RC1( "SetLed1", 	_VOI, BSP_SetLed1,	 		_I08,	"SetLed1: 0:low,1:high,2:toggle"),
//UFD_RC1( "SetLed2", 	_VOI, BSP_SetLed2,	 		_I08,	"SetLed2: 0:low,1:high,2:toggle"),
//UFD_RC1( "SetLcdBl", 	_VOI, BSP_SetLcdBl,	 		_I08,	"SetLcdBackLight: 0:off,1:on,2:toggle"),
//UFD_RC1( "SetRO1", 		_VOI, RO1_Enable,	 		_U08,	"SetRO1: 0:low,1:high"),	
//UFD_RC1( "SetRO2", 		_VOI, RO2_Enable,	 		_U08,	"SetRO2: 0:low,1:high"),
//UFD_RC0( "GetDI", 		_U08, DI_Scan,	 			        "GetDI"),
//UFD_RC0( "GetKey", 		_U08, KEY_Scan,	 			        "GetKey"),
//UFD_RC0( "SwdEnable",   _VOI, BSP_EnableSwd,                "Set SWD function on"),
//UFD_END()
//};



void BSP_GpioInit(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);//使能PB端口时钟
	/********************************************************************/
	///< 端口方向配置->输出
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		//推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		//IO口速度为50MHz
	
	GpioCfgOutput(PORT_LED0,PIN_LED0, GPIO_InitStructure,Bit_RESET);					 			
	GpioCfgOutput(PORT_LED1,PIN_LED1, GPIO_InitStructure,Bit_SET);	
	/********************************************************************/
}




void BSP_SetLed0(uint8_t bVal)
{
	if(bVal<1)
		GPIO_WriteBit(PORT_LED0,PIN_LED0,Bit_RESET);
	else if(bVal<2)
		GPIO_WriteBit(PORT_LED0,PIN_LED0,Bit_SET);
	else
		Gpio_ToggleOutputIO(PORT_LED0,PIN_LED0);
}
void BSP_SetLed1(uint8_t bVal)
{
	if(bVal<1)
		GPIO_WriteBit(PORT_LED1,PIN_LED1,Bit_RESET);
	else if(bVal<2)
		GPIO_WriteBit(PORT_LED1,PIN_LED1,Bit_SET);
	else
		Gpio_ToggleOutputIO(PORT_LED1,PIN_LED1);
}

